﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using util;

namespace WebApplication1.dormitoryAdministrator
{
    public partial class dormitoryAdministrator : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {   
            if (!Page.IsPostBack) {
                   Panel1.Visible = false;
            }
            Panel2.Visible = false;
            String roleName = (String)Application["rolename"];
            if (roleName != "宿舍管理员")
            {
                Response.Write("<script>alert('没有权限查看此页面')</script>");
                Server.Transfer("\\login\\login.aspx");
            }
            else {
                username.Text = Application["username"].ToString();
                String sql = "SELECT h.name FROM `user` u INNER JOIN `hostel` h on h.manager_id = u.id and u.username = '" + Application["username"].ToString() + "'";
                DataTable Adt = MySqlHelper.GetDataTable(sql);
                String hostel = Adt.Rows[0][0].ToString();
                Application["hostel"] = hostel;
            }
           
            
        }

        protected void Image2_Click(object sender, ImageClickEventArgs e)
        {
            bool a =  Panel1.Visible;
            a = !a;
            Panel1.Visible = a;
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Server.Transfer("\\login\\login.aspx");
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Panel2.Visible = true;
        }

        protected void okAdd_Click(object sender, EventArgs e)
        {
            String oldpass = oldpassword.Text;
            String newpass = newpassword.Text;
            String epass = epasssword.Text;
            int id = (int)Application["userId"];
            String username = (String)Application["username"];
            String sql2 = "SELECT * FROM `user` WHERE username = '"+ username + "'and `password` ='" + username + "'";
            DataTable table = MySqlHelper.GetDataTable(sql2);
            if (table.Rows.Count == 0)
            {
                Response.Write("<script>alert('密码错误，请重新输入！')</script>");
            }
            else {
                String sql = "UPDATE `user` set `password` = '" + newpass + "' WHERE id = " + id;
                DataTable Adt = MySqlHelper.GetDataTable(sql);
                Response.Write("<script>alert('修改成功，请重新登入！')</script>");
                Server.Transfer("\\login\\login.aspx");
            }
        }

        protected void escAdd_Click(object sender, EventArgs e)
        {
            this.Panel2.Visible = false;
        }
    }
}